# -*- coding: utf-8 -*-
"""
Created on Thu Sep 24 10:48:40 2020

@author: juliang
"""

from AlphaFactor import AlphaModel
import datetime

# In[0]： 参数设定
stock_codes = '000905.SH'     # 投资资产的代码
ind_codes = ['indi1','indi2','indi3','indi4','indi5']     # 因子代码
begdate = '20200101'      # 开始研究日期
enddate = '20200922'      # 最终研究日期

# In[1]: 因子筛选与合成
# 实例化模型类
am = AlphaModel(stock_codes,ind_codes,begdate,enddate,
                        period=1,bench_code='000905.SH',is_index = True,
                        include_st = False,include_suspend = False,
                        include_new_stock = False,ipo_days = 60)
# 不分组
indicator_outcome1 = am.run_single_indicator_analysis(ind_codes[0])   # 计算单因子统计数据
indicator_outcome2 = am.run_multi_indicators_analysis(ind_codes[:4])   # 计算多因子统计数据

# 分组
am.set_groupby()
indicator_outcome1 = am.run_single_indicator_analysis(ind_codes[0])   # 计算单因子统计数据
indicator_outcome2 = am.run_multi_indicators_analysis(ind_codes[:4])   # 计算多因子统计数据

# In[2]: 根据因子进行选股
datestr = datetime.datetime(2020,9,1).strftime('%Y%m%d')
datelist=['20200911','20200918','20200922']
stock_num=10
select_ind=['indi1','indi2','indi3','indi4']
lookbackPeriod=5

'''按照打分法进行选股'''
am.set_model_type('score')
# 传入一个日期，不行业分类
stocklist = am.select_stocks_by_model(datestr,stock_num,by_group = False)
# 传入一个日期，行业分类
stocklist1 = am.select_stocks_by_model(datestr,stock_num,by_group = True)
# 传入一个日期序列，不行业分类
stock_choosed = am.select_stocks_by_model(datelist,stock_num,by_group = False,select_ind = None,lookbackPeriod = 50)
# 传入一个日期序列，行业分类
stock_choosed1 = am.select_stocks_by_model(datelist,stock_num,by_group = True,select_ind = None,lookbackPeriod = 50)

'''按照回归法进行选股'''
am.set_model_type('regress')
# 传入一个日期，不行业分类
stocklist = am.select_stocks_by_model(datestr,stock_num,by_group = False)
# 传入一个日期，行业分类
stocklist1 = am.select_stocks_by_model(datestr,stock_num,by_group = True)
# 传入一个日期序列，不行业分类
stock_choosed = am.select_stocks_by_model(datelist,stock_num,by_group = False,select_ind = None,lookbackPeriod = 50)
# 传入一个日期序列，行业分类
stock_choosed1 = am.select_stocks_by_model(datelist,stock_num,by_group = True,select_ind = None,lookbackPeriod = 50)

'''赋权重'''
stock_choosed_df = am.add_weight_to_stock_choose()

'''数据保存'''
#stock_choosed_df.to_csv('stock_choosed.csv')

#测试用
#stock = am.stock.copy(deep = True)
#select_ind = am.select_ind
#select_factors = am.factors[select_ind]
#
#test_factor=am.factor









